package com.gss.armory.master;

import java.util.Collection;
import java.util.HashSet;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.gss.common.domain.CommonDetailsDTO;
import com.gss.common.util.ApplicationMaps;

@Entity
@Table(name="LC_USER_DETAILS")
public class UserDetailsAuthDTO extends CommonDetailsDTO implements UserDetails {

	private static final long serialVersionUID = 4252923265521334091L;
	
	@NotBlank
	private String username;
	@NotBlank
	//@Pattern(regexp=ApplicationConstants.REGEX_FOR_PASSWORD , message = "Password not in the standard format (Password should contain One Uppercase[A-Z], One Lowercase[a-z], One Digit[0-9], one Special Character[!@#$%^&])")
	private String password;
	private String txtPassword; 

	private boolean isAccountNonExpired;
	private boolean isAccountNonLocked;
	private boolean isCredentialsNonExpired;
	private boolean isEnabled;
	private boolean credentialsExpired;
	private Integer count;



	private Collection<GrantedAuthority> authorities = new HashSet<GrantedAuthority>() ;
	@NotBlank
	private String userDisplayName;
	private String curRole;
	
	@Email
	@NotBlank
	private String strEmailId;
	private Long locationId;
	private String []role;
	private String strDeptt;
	private Long depttId;
	private String strLocation;
	private String contactNumDesk;
	private String contactNumMob;
	
	private String deptName;
	private String locName;
	private Integer cdId;
	
	private Collection<GrantedAuthority> additionalAuthorities = new HashSet<GrantedAuthority>() ;

	@ManyToMany(targetEntity=RoleDetailsAuthDTO.class,fetch=FetchType.EAGER)
	@JoinTable(name="LC_USER_ROLE_MAPPING",
			joinColumns=@JoinColumn(name="URM_USER_ID"),
			inverseJoinColumns=@JoinColumn(name="URM_ROLE_ID")
			)
	public Collection<GrantedAuthority> getAuthorities() {
		// TODO Auto-generated method stub
		return authorities;
	}

	@Basic
	@Column(name="UD_PASSWORD",length=100,nullable=false)
	public String getPassword() {
		// TODO Auto-generated method stub
		return password;
	}

	@Id
	@Column(name="UD_USER_ID",length=20)
	public String getUsername() {
		// TODO Auto-generated method stub
		return username;
	}

	public void validate() {
		if(getUsername().toString()==getPassword().toString()){
			
		}
		
	}
	
	
	@Column(name="UD_IS_ACNT_NON_EXPD",nullable=false,columnDefinition="smallint default 1")
	public boolean isAccountNonExpired() {
		// TODO Auto-generated method stub
		return isAccountNonExpired;
	}
	
/*	@Transient
	public boolean getIsAccountNonExpired() {
		// TODO Auto-generated method stub
		return isAccountNonExpired;
	}
*/

	@Column(name="UD_IS_ACNT_NON_LOCKD",nullable=false,columnDefinition="smallint default 1")
	public boolean isAccountNonLocked() {
		// TODO Auto-generated method stub
		return isAccountNonLocked;
	}
	
	/*@Transient
	public boolean getIsAccountNonLocked() {
		// TODO Auto-generated method stub
		return isAccountNonLocked;
	}*/
	
	@Column(name="UD_IS_CRED_NON_EXPD",nullable=false,columnDefinition="smallint default 1")
	public boolean isCredentialsNonExpired() {
		// TODO Auto-generated method stub
		return isCredentialsNonExpired;
	}
	
	/*@Transient
	public boolean getIsCredentialsNonExpired() {
		// TODO Auto-generated method stub
		return isCredentialsNonExpired;
	}*/

	
	@Column(name="UD_IS_ENABLED", nullable=false,columnDefinition="smallint default 1")
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return isEnabled;
	}
	
	/*@Transient
	public boolean getIsEnabled() {
		// TODO Auto-generated method stub
		return isEnabled;
	}*/

	
	@Column(name="UD_USER_NAME",length=50, nullable=false)
	public String getUserDisplayName() {
		return userDisplayName;
	}

	public void setUserDisplayName(String userDisplayName) {
		this.userDisplayName = userDisplayName;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setAccountNonExpired(boolean isAccountNonExpired) {
		this.isAccountNonExpired = isAccountNonExpired;
	}

	public void setAccountNonLocked(boolean isAccountNonLocked) {
		this.isAccountNonLocked = isAccountNonLocked;
	}

	public void setCredentialsNonExpired(boolean isCredentialsNonExpired) {
		this.isCredentialsNonExpired = isCredentialsNonExpired;
	}

	public void setEnabled(boolean isEnabled) {
		this.isEnabled = isEnabled;
	}
	
	/*public void setIsAccountNonExpired(boolean isAccountNonExpired) {
		this.isAccountNonExpired = isAccountNonExpired;
	}

	public void setIsAccountNonLocked(boolean isAccountNonLocked) {
		this.isAccountNonLocked = isAccountNonLocked;
	}

	public void setIsCredentialsNonExpired(boolean isCredentialsNonExpired) {
		this.isCredentialsNonExpired = isCredentialsNonExpired;
	}

	public void setIsEnabled(boolean isEnabled) {
		this.isEnabled = isEnabled;
	}*/

	public void setAuthorities(Collection<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}


@Transient
	public String getCurRole() {
		return curRole;
	}



	public void setCurRole(String curRole) {
		this.curRole = curRole;
	}


@Column(name="UD_EMAIL_ID")
	public String getStrEmailId() {
		
		return this.strEmailId;
	}



	public void setStrEmailId(String strEmailId) {
		this.strEmailId = strEmailId;
	}


@Transient
	public Collection<GrantedAuthority> getAdditionalAuthorities() {
		return additionalAuthorities;
	}



	public void setAdditionalAuthorities(
			Collection<GrantedAuthority> additionalAuthorities) {
		this.additionalAuthorities = additionalAuthorities;
	}


	@Column(name="UD_LOC_ID")
	public Long getLocationId() {
		return locationId;
	}


	@Column(name="UD_DEPTT_ID")
	public Long getDepttId() {
		return depttId;
	}



	public void setLocationId(Long locationId) {
		this.locationId = locationId;
		this.setStrLocation(ApplicationMaps.LOCID_LOCNAME_MAP.get(locationId));
	}



	public void setDepttId(Long depttId) {
		this.depttId = depttId;
		this.setStrDeptt(ApplicationMaps.DEPTID_DEPTSNAME_MAP.get(depttId));
	}

	@Transient
	public String getStrDeptt() {
		return strDeptt;
	}
	public void setStrDeptt(String strDeptt) {
		this.strDeptt = strDeptt;
	}


	@Transient
	public String getStrLocation() {
		return strLocation;
	}



	public void setStrLocation(String strLocation) {
		this.strLocation = strLocation;
	}


	@Transient
	public String[] getRole() {
		return role;
	}



	public void setRole(String[] role) {
		this.role = role;
	}


	@Column(name="UD_CONTACT_NUM1")
	public String getContactNumDesk() {
		return contactNumDesk;
	}


	@Column(name="UD_CONTACT_NUM2")
	public String getContactNumMob() {
		return contactNumMob;
	}

	public void setContactNumDesk(String contactNumDesk) {
		this.contactNumDesk = contactNumDesk;
	}

	public void setContactNumMob(String contactNumMob) {
		this.contactNumMob = contactNumMob;
	}
	
	@Transient
	public boolean isCredentialsExpired() {
		return credentialsExpired;
	}

	public void setCredentialsExpired(boolean credentialsExpired) {
		this.credentialsExpired = credentialsExpired;
	}
	
	@Column(name="UD_COUNT", nullable=true)
	public Integer getCount() {
		return count;
	}

	public void setCount(Integer count) {
		this.count = count;
	}

	
	@Column(name = "UD_CD_ID")
	public Integer getCdId() {
		return cdId;
	}

	public void setCdId(Integer cdId) {
		this.cdId = cdId;
	}
	
	@Override
	public String toString() {
		return "UserDetailsAuthDTO [authorities=" + authorities.size()
				+ ", isAccountNonExpired=" + isAccountNonExpired
				+ ", isAccountNonLocked=" + isAccountNonLocked
				+ ", isCredentialsNonExpired=" + isCredentialsNonExpired
				+ ", isEnabled=" + isEnabled + ", password=" + password
				+ ", userDisplayName=" + userDisplayName + ", username="
				+ username + ", count=" + count + "]";
	}
	
	@Transient
	public String getTxtPassword() {
		return txtPassword;
	}

	public void setTxtPassword(String txtPassword) {
		this.txtPassword = txtPassword;
	}
	
	@Transient
	public String getDeptName() {
		if(null != depttId)
			return ApplicationMaps.DEPTID_DEPTSNAME_MAP.get(depttId);
		else
			return "";
	}

	@Transient
	public String getLocName() {
		if(null != locationId)
			return ApplicationMaps.LOCID_LOCNAME_MAP.get(locationId);
		else
			return "";
	}
}
